<?php

class Reactive1Evaluator {

    public static function evaluate($submission){
        //define("EVALUATOR_DEBUG", true);
        $problem = $submission->problem;
        $time_limit = $problem->getConfig('time_limit') / 1000;
        $memory_limit = $problem->getConfig('memory_limit') / 1024;
        $testcases = $problem->getConfig('testcases');

        $cids = explode(" " , $problem->getConfig('sample_contest'));
        $isSampleOnly = in_array($submission->contest_id, $cids) && $submission->contest_id !== NULL;

        //Compile
        $source_content = $submission->getSubmitContent('source_content');
        $source_lang = $submission->getSubmitContent('source_lang');
        $source_path = Sandbox::getTempName();
        file_put_contents($source_path, $source_content);
        $compiler_output = "";
        try {
            $execpath = Compiler::compile($source_path, $source_lang, NULL, NULL, $compiler_output);
        } catch (CompilerException $ex) {
            $submission->setGradeContent("verdict", "Compile Error");
            $submission->setGradeContent("output", $compiler_output);
            if (!defined("EVALUATOR_DEBUG")) unlink($source_path);
            return;
        }
        if (!defined("EVALUATOR_DEBUG")) unlink($source_path);

        //Execute samples
        $isruntimeok = true;
        $istimelimit = false;
        $rightoutput = true;
        $err = "";
        $output = "";
        $i = 1;
        $total_testcase = 0;
        $right_testcase = 0;
        foreach($testcases as $testcase)
        {
            if (!isset($testcase['sample']))
                continue;

            $total_testcase++;
            $output .= "Testcase #".$i."\n";$i++;
            $inputpath = $problem->getFile('evaluator/files/'.$testcase['input']);
            
            //$outputpath = $problem->getFile('evaluator/files/'.$testcase['output']);
            //$tmpoutput = Sandbox::getTempName();
            //$sandboxparam = "-e -f -a 2 -w $time_limit -m $memory_limit -i $inputpath -o $tmpoutput";
            //$execoutput = "";
            
            //RUN
            
            
            $runpath = str_replace('[PROBLEM_PATH]' , $problem->getFile('evaluator/files/') , $problem->getConfig('command_line'));
            $runpath = str_replace('[SOLUTION]' , $execpath , $runpath);
            $runpath = str_replace('[TESTCASE]' , $inputpath , $runpath);
            
            echo $runpath . "\n";
            
            $result = exec($runpath);
            
            $output .= $result . "\n";
            
            if (substr($result , 0 , 4) == '[OK]')
                    $right_testcase++;
            else if ($result == '[FAILED] Program terminated unexpectedly') {
                    $isruntimeok = false;
                    $err = 'Runtime error';
            }
            else if (substr($result , 0 , 8) == '[FAILED]')
                    $rightoutput = false;
			
            
            if (file_exists($tmpoutput) && !defined("EVALUATOR_DEBUG")) unlink($tmpoutput);
        }
        $verdict = ($isruntimeok) ? (($rightoutput) ? "Accepted" : "Wrong Answer") : ($err);
        $submission->verdict = $verdict;
        $submission->setGradeContent("verdict", $verdict);
        $submission->setGradeContent("output", $output);
        $submission->setGradeContent("total_testcase", $total_testcase);
        $submission->setGradeContent("right_testcase", $right_testcase);
        $submission->score = ($total_testcase == 0) ? 0 : (($right_testcase / $total_testcase) * 100);

        //Execute offical TC
        $isruntimeok = true;
        $istimelimit = false;
        $rightoutput = true;
        $err = "";
        $output = "";
        $i = 1;
        $total_testcase = 0;
        $right_testcase = 0;
        foreach($testcases as $testcase)
        {
            if (isset($testcase['sample']))
                continue;

            $total_testcase++;
            $output .= "Testcase #".$i."\n";$i++;
            $inputpath = $problem->getFile('evaluator/files/'.$testcase['input']);

            //$outputpath = $problem->getFile('evaluator/files/'.$testcase['output']);
            //$tmpoutput = Sandbox::getTempName();
            //$sandboxparam = "-e -f -a 2 -w $time_limit -m $memory_limit -i $inputpath -o $tmpoutput";
            //$execoutput = "";

            //RUN


            $runpath = str_replace('[PROBLEM_PATH]' , $problem->getFile('evaluator/files/') , $problem->getConfig('command_line'));
            $runpath = str_replace('[SOLUTION]' , $execpath , $runpath);
            $runpath = str_replace('[TESTCASE]' , $inputpath , $runpath);

            echo $runpath . "\n";

            $result = exec($runpath);

            $output .= $result . "\n";

            if (substr($result , 0 , 4) == '[OK]')
                    $right_testcase++;
            else if ($result == '[FAILED] Program terminated unexpectedly') {
                    $isruntimeok = false;
                    $err = 'Runtime error';
            }
            else if (substr($result , 0 , 8) == '[FAILED]')
                    $rightoutput = false;


            if (file_exists($tmpoutput) && !defined("EVALUATOR_DEBUG")) unlink($tmpoutput);
        }

        if (!$isSampleOnly) {
            $verdict = ($isruntimeok) ? (($rightoutput) ? "Accepted" : "Wrong Answer") : ($err);
            $submission->verdict = $verdict;
            $submission->setGradeContent("verdict", $verdict);
            $submission->setGradeContent("output", $output);
            $submission->setGradeContent("total_testcase", $total_testcase);
            $submission->setGradeContent("right_testcase", $right_testcase);
            $submission->score = ($total_testcase == 0) ? 0 : (($right_testcase / $total_testcase) * 100);
        }
        else {
            $submission->setGradeContent("official_result", ($total_testcase == 0) ? 0 : (($right_testcase / $total_testcase) * 100));
            $submission->setGradeContent("official_output", $output);
        }
        
        if (file_exists($execpath) && !defined("EVALUATOR_DEBUG")) unlink($execpath);
    }
    
}
